
import store from '@/store';
export default {
	data () {
		return {
			socket: "",
			websocket: "wss://socket.peixiua.com:9502",
		}
	},
	methods: {
		broadcast (text) {
			const speech = new SpeechSynthesisUtterance(text);
			speech.volume = 1; // 设置音量
			window.speechSynthesis.speak(speech);
		},
		connect () {
			if (typeof (WebSocket) === "undefined") {
				this.$message.error("您的浏览器不支持socket")
			} else {
				// 实例化socket
				this.socket = new WebSocket(this.websocket)
				// 监听socket连接
				this.socket.onopen = this.open
				// 监听socket错误信息
				this.socket.onerror = this.error
				// 监听socket消息
				this.socket.onmessage = this.message
			}
		},
		disconnect () {
			this.$request.del("/live/unbind")
		},
		open () {

		},
		error () {
			this.$message.error("socket发生错误")
		},
		// closeLive () {
		// 	if (this.player) {
		// 		this.player.dispose()
		// 	}
		// 	this.player = null
		// 	this.visible = false
		// },
		message (value) {
			let that = this
			let data = JSON.parse(value.data)
			data.rand_num = Math.random()
			switch (data.type) {
				case 'init':
					this.$request.post("/live/bind_client", { client_id: data.client_id }).then((res) => {
						console.log(res)
					})
					break;
				case 'text':
				case 'access':
				case 'leave':
				case 'gift':
					this.$store.state.live.message = data
					break;


				case 'close':
					this.$message({
						message: '直播已结束',
						type: 'error',
						customClass: 'super-message',
					})
					// this.closeLive()
					this.$store.state.system.drop = 1
					break;
				case 'kick_out':
					this.$message({
						message: '您已被主播踢出房间！',
						type: 'error',
						customClass: 'super-message',
					})
					// this.closeLive()
					this.$store.state.system.drop = 2
					break;
			}
		}

	}
}